#include <CSLP-D/CSLP.h>
#include <stdio.h>
#include <unistd.h>

#define MAX_LOOP 1000
#define SLEEP CSLP_Sleep(1)

void* foo(void *data)
{
  int i;
  CSLP_MUTEX *mutex = (CSLP_MUTEX*)(data);
  for (i = 0; i < MAX_LOOP; i++)
  {
    CSLP_WaitToLockMutex(mutex);
    printf("I'm ");
    SLEEP;
    printf("foo!!!\t");
    CSLP_UnlockMutex(mutex);
    SLEEP;
  }
}

int main(int argc, char **argv)
{
  int i;
  CSLP_THREAD thread;
  CSLP_MUTEX mutex;
  CSLP_InitMutex(&mutex);
  CSLP_CreateThread(&thread, foo, &mutex);
  for (i = 0; i < MAX_LOOP; i++)
  {
    CSLP_WaitToLockMutex(&mutex);
    printf("I'm ");
    SLEEP;
    printf("main!!\t");
    CSLP_UnlockMutex(&mutex);
    SLEEP;
  }
  CSLP_WaitThread(thread);
  CSLP_DeleteMutex(&mutex);
  putchar('\n');
  return 0;
}

